﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Data.GameData;
using Data.GameData.Utils;
using Data.GameData.Interfaces;
using WeifenLuo.WinFormsUI.Docking;

namespace GUIFramework
{
    public class EventListener
    {
        public EventListener()
        {
            Initialise();
        }

        private void Initialise()
        {
            Race.RaiseActiveRaceChangedEvent += OnActiveRaceChangedEvent;
            Race.RaiseNewStarSystemEvent += UpdateGamePanels;

            Race.RaiseRaceChangedEvent += UpdateGamePanels;
            Race.RaiseNewOrderEvent += UpdateGamePanels;
            StarSystemInformation.RaiseSystemInfomationChangedEvent += UpdateGamePanels;

            Game.RaiseNewTurnEvent += UpdateGamePanels;
        }

        private void UpdatePanels(BasicEventArgs e)
        {
            foreach (IDockContent doc in MainForm.MainDockPanel.Contents.ToArray())
            {
                IGamePanel panel = doc as IGamePanel;
                if (panel != null)
                    panel.UpdatePanel(e);
            }
        }

        private void OnActiveRaceChangedEvent(object sender, BasicEventArgs e)
        {
            Commands.CloseAllWindowsExcept(e.Ident);
            Commands.ResetPanesFor(e.Ident);

            if (Race.ActiveRace != null && Race.ActiveRace.HomeSystem != null)
                Commands.OpenView(Race.ActiveRace.HomeSystem.MapId, ViewType.vtMAP_STRATEGIC);
        }

        private void UpdateGamePanels(object sender, BasicEventArgs e)
        {
            UpdatePanels(e);
        }
    }
}
